clear all
clc 

stataPath = '"C:\Program Files\Stata18\StataSE-64.exe"'; % Define your Stata path 
runsection=0; % Set it to 1 if you want to start from raw data, 0 if you want some pre-calibrated data. (Take care if 1 it might take several days).
rscriptPath = '"C:/Program Files/R/R-4.4.1/bin/Rscript.exe"'; % Define your Stata path 
save('./input/rscriptPath.mat', 'rscriptPath');
save('./input/statapath.mat', 'stataPath');
save('./input/runsection.mat', 'runsection');


%% Step 0.1: Copy and rename the original files in RawData
% -------------------------------------------------------------------------

% Define the source directory (where the original files are stored)
source_folder = fullfile(pwd, 'RawData');

% List of files and their new names (original -> renamed copy)
file_mapping = {
    'EU_Historical database _yoy_Oct__2024.xlsm',  'EZ_HistoricalDatabase_Y_Oct_2024.xlsm';
    'Euro_Historical database _zero_Oct_2024.xlsm', 'EZ_HistoricalDatabase_Z_Oct_2024.xlsm';
    'Historical database _zero_Oct_2024.xlsm',      'US_HistoricalDatabase_Z_Oct_2024.xlsm';
    'Historical database _yoy_Oct_2024.xlsm',       'US_HistoricalDatabase_Y_Oct_2024.xlsm';
};

% Loop over the files and create renamed copies in the same folder
for i = 1:size(file_mapping, 1)
    original_file = fullfile(source_folder, file_mapping{i, 1});
    renamed_copy  = fullfile(source_folder, file_mapping{i, 2});
    
    if exist(original_file, 'file')
        copyfile(original_file, renamed_copy);
        fprintf('Copied and renamed file: %s -> %s\n', file_mapping{i, 1}, file_mapping{i, 2});
    else
        fprintf('File not found (not copied): %s\n', file_mapping{i, 1});
    end
end

fprintf('Step 1 completed: Files copied and renamed in RawData.\n\n');



%% Step 0.2: Process selected files from RawData and copy to DataForStata
% -------------------------------------------------------------------------

% Define the relative paths for input (RawData) and output (DataForStata)
input_directory  = fullfile(pwd, 'RawData');
output_directory = fullfile(pwd, 'input');

% List of specific renamed .xlsm files to process
file_list = {
    'US_HistoricalDatabase_Y_Oct_2024.xlsm', 
    'US_HistoricalDatabase_Z_Oct_2024.xlsm',
    'EZ_HistoricalDatabase_Y_Oct_2024.xlsm'
};

% Define the processing functions folder
processing_folder = fullfile(pwd, 'functionsRA');
addpath(processing_folder);

% Ensure the output directory exists
if ~exist(output_directory, 'dir')
    mkdir(output_directory);
end

% Loop over the selected files and process them
for f = 1:length(file_list)
    input_file = fullfile(input_directory, file_list{f});
    
    if exist(input_file, 'file')
        fprintf('Processing file: %s\n', file_list{f});

        % Call the appropriate processing function with the correct arguments
        if contains(file_list{f}, 'US_HistoricalDatabase_Y_Oct_2024')
            process_US_historical_Y(input_file, output_directory);
        elseif contains(file_list{f}, 'US_HistoricalDatabase_Z_Oct_2024')
            process_US_historical_Z(input_file, output_directory);
        elseif contains(file_list{f}, 'EZ_HistoricalDatabase_Y_Oct_2024')
            process_EZ_historical_Y(input_file, output_directory);
        else
            fprintf('No specific function assigned for: %s\n', file_list{f});
        end

    else
        fprintf('File not found (not processed): %s\n', file_list{f});
    end
end




%% Step 0.3: Copy the additional file (no processing) from RawData to DataForStata
% -------------------------------------------------------------------------

% The file you want to copy without processing:
additional_file = 'EZ_HistoricalDatabase_Z_Oct_2024.xlsm';
additional_filename = fullfile(input_directory, additional_file);

if exist(additional_filename, 'file')
    copyfile(additional_filename, fullfile(output_directory, additional_file));
    fprintf('Copied additional file (no processing) to input: %s\n', additional_file);
else
    fprintf('Additional file not found: %s\n', additional_file);
end

fprintf('\nProcessing completed for all selected files.\n');






%% Step 1: Enabling Macro's on excel on the computer + Setting the different Excel file for Macro "Makro" execution




clear all
clc
baseDir = pwd; % Get the directory where the script is running
stataDir = fullfile(baseDir, 'input'); % Folder containing Stata & Excel files

% Modify Excel Trust Center settings via Windows Registry
try
    % Define registry path for macro settings
    regPath = 'HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Security';

    % Modify registry keys to enable macros
    system(['reg add "' regPath '" /v VBAWarnings /t REG_DWORD /d 1 /f']); % 1 = Enable all macros
    system(['reg add "' regPath '" /v AccessVBOM /t REG_DWORD /d 1 /f']); % Allow access to VBA project

    disp('Excel macros enabled successfully. Restart Excel for changes to take effect.');

catch ME
    disp(['Error modifying registry: ' ME.message]);
end

% Define pairs of {Macro Workbook, Source Workbook}
excelPairs = {

    fullfile(stataDir, 'US_distributions_formatted_Z_pt2.xlsm'), ...
    fullfile(stataDir, 'US_HistoricalDatabase_Z_Oct_2024.xlsm'); % Same source

    fullfile(stataDir, 'EZ_distributions_formatted_Y.xlsm'), ...
    fullfile(stataDir, 'EZ_HistoricalDatabase_Y_Oct_2024.xlsm');

    fullfile(stataDir, 'EZ_distributions_formatted_Z.xlsm'), ...
    fullfile(stataDir, 'EZ_HistoricalDatabase_Z_Oct_2024.xlsm');

    fullfile(stataDir, 'US_distributions_formatted_Y.xlsm'), ...
    fullfile(stataDir, 'US_HistoricalDatabase_Y_Oct_2024.xlsm'); % Same source

    fullfile(stataDir, 'US_distributions_formatted_Z.xlsm'), ...
    fullfile(stataDir, 'US_HistoricalDatabase_Z_Oct_2024.xlsm'); % Same source


};

%% Run the Macro "MAKRO"
macroName = 'MAKRO'; % Ensure this matches exactly

% ======= STEP 2: Open Excel through ActiveX =======
excel = actxserver('Excel.Application');
excel.DisplayAlerts = false; % Suppress popups
excel.AutomationSecurity = 2; % Lower security to allow macros
excel.Visible = true; % Run in the background

try
    for i = 1:size(excelPairs, 1)
        macroWorkbook = excelPairs{i, 1}; % Macro workbook
        sourceWorkbook = excelPairs{i, 2}; % Source workbook

        try
            % ======= STEP 3: Open Workbooks with Read/Write Access =======
            wb_source = excel.Workbooks.Open(sourceWorkbook, 0, false);
            fprintf(' Opened source workbook: %s\n', sourceWorkbook);

            wb_macro = excel.Workbooks.Open(macroWorkbook, 0, false);
            fprintf(' Opened macro workbook: %s\n', macroWorkbook);

            % =======  Ensure Correct Source is Active =======
            wb_source.Activate;
            pause(1); % Small delay before running macro

            % =======  Ensure Macro Workbook Has Sheets & Clear Data =======
            if wb_macro.Sheets.Count > 0
                ws = wb_macro.Sheets.Item(1);
                fprintf(' Sheet selected in %s\n', macroWorkbook);
                ws.Range('A2:H200000').ClearContents;
            else
                error('�? No sheets found in %s.', macroWorkbook);
            end

            % =======  Save Before Running Macro =======
            try
                wb_macro.Save;
                fprintf(' Saved changes in: %s\n', macroWorkbook);
            catch ME
                fprintf('Warning: Could not save %s before running macro. Error: %s\n', macroWorkbook, ME.message);
            end

            % =======  Run the Macro =======
            try
                macroFullName = sprintf('%s!%s', wb_macro.Name, macroName);
                wb_macro.Application.Run(macroFullName);
                fprintf(' Macro ran successfully on: %s\n', macroWorkbook);
            catch ME
                fprintf('Error running macro on %s: %s\n', macroWorkbook, ME.message);
                continue; % Skip to the next pair if there's an issue
            end

            % ======= STEP 8: Save and Close Workbooks =======
            wb_macro.Save;
            wb_macro.Close;
            wb_source.Save;
            wb_source.Close;
            fprintf(' Processing completed for: %s\n', macroWorkbook);

        catch ME
            fprintf(' Error processing %s: %s\n', macroWorkbook, ME.message);
        end
    end

    % Quit Excel
    excel.Quit;
    delete(excel);
    disp(' All Excel files processed successfully.');

catch ME
    fprintf(' Fatal error: %s\n', ME.message);
    excel.Quit;
    delete(excel);
end









%%
% ======= STEP 3: Run Stata .do Files with Relative Paths =======
baseDir = pwd; % Get the directory where the script is running
stataDir = fullfile(baseDir, 'functionsRA'); % Folder containing Stata & Excel files



% Define paths to your Stata .do file
doFile1 = fullfile(stataDir, 'monthly_EZ.do');
doFile2= fullfile(stataDir, 'monthly_US.do');

% Define Stata executable path (modify according to your installation)
load('./input/statapath.mat', 'stataPath');
% Ensure MATLAB switches to the correct directory before launching Stata
cd(stataDir);

% Run the first .do file
command1 = sprintf('%s /e do "%s"', stataPath, doFile1);
status1 = system(command1);

% Check if the first script ran successfully
if status1 == 0
    disp('Stata monthly EZ script ran successfully.');
else
    disp('Error running Stata EZ script.');
end


% Run the second .do file
command2 = sprintf('%s /e do "%s"', stataPath, doFile2);
status2 = system(command2);

% Check if the first script ran successfully
if status2 == 0
    disp('Stata monthly US script ran successfully.');
else
    disp('Error running Stata US script.');
end



%%
cd ..
baseDir =pwd;% Get the directory where the script is running

% ======= STEP 4: Run Additional MATLAB Scripts =======

% Define the directory where the scripts are located
scriptsDir = fullfile(baseDir, 'functionsRA');

% Add the folder to MATLAB path temporarily
addpath(scriptsDir);

% Run the first MATLAB script
try
    run(fullfile(scriptsDir, 'read_data_EZ.m'));  % Run script from functionsRA folder
    disp('First MATLAB script ran successfully.');
catch ME
    fprintf('Error running first MATLAB script: %s\n', ME.message);
end


baseDir = pwd; % Get the directory where the script is running

% Define the directory where the scripts are located
scriptsDir = fullfile(baseDir, 'functionsRA');

% Add the folder to MATLAB path temporarily
addpath(scriptsDir);

% Run the second MATLAB script
try
    run(fullfile(scriptsDir, 'read_data_US.m'));  % Run script from functionsRA folder
    disp('Second MATLAB script ran successfully.');
catch ME
    fprintf('Error running second MATLAB script: %s\n', ME.message);
end




%% OPTIONAL
clear all
% ======= STEP 4.5: Preparing the pis parameter =======
load('./input/runsection.mat', 'runsection');


if runsection==1
baseDir = pwd; % Get the directory where the script is running
scriptsDir = fullfile(baseDir, 'functionsRA');
addpath(scriptsDir);

% Change directory to where the scripts are located
cd(baseDir);

% Run the first MATLAB script
try
    run(fullfile(scriptsDir,'mdl101_EZ_mid.m'));  
    disp('First MATLAB script ran successfully.');
catch ME
    fprintf('Error running first MATLAB script: %s\n', ME.message);
end
baseDir = pwd; % Get the directory where the script is running


% Define the directory where the scripts are located
scriptsDir = fullfile(baseDir, 'functionsRA');

% Add the folder to MATLAB path temporarily
addpath(scriptsDir);

% Run the second MATLAB script
try
    run(fullfile(scriptsDir,'mdl101_US_mid.m'));  
    disp('Second MATLAB script ran successfully.');
catch ME
    fprintf('Error running second MATLAB script: %s\n', ME.message);
end


end 





%%

% ======= STEP 5: Run Additional LONG MATLAB Scripts =======

baseDir = pwd; % Get the directory where the script is running
scriptsDir = fullfile(baseDir, 'functionsRA');
addpath(scriptsDir);

% Change directory to where the scripts are located
cd(baseDir);

% Run the first MATLAB script
try
    run(fullfile(scriptsDir,'mdl101_EZ_month_quart.m'));  
    disp('First MATLAB script ran successfully.');
catch ME
    fprintf('Error running first MATLAB script: %s\n', ME.message);
end
baseDir = pwd; % Get the directory where the script is running


% Define the directory where the scripts are located
scriptsDir = fullfile(baseDir, 'functionsRA');

% Add the folder to MATLAB path temporarily
addpath(scriptsDir);

% Run the second MATLAB script
try
    run(fullfile(scriptsDir,'mdl101_US_month_quart.m'));  
    disp('Second MATLAB script ran successfully.');
catch ME
    fprintf('Error running second MATLAB script: %s\n', ME.message);
end





%%

% ======= STEP 6: Run Graphs time monthquart =======
% Change directory to where the scripts are located
baseDir = pwd; % Get the directory where the script is running

cd(baseDir);

scriptDir = fullfile(baseDir, 'functionsRA'); % Define the folder containing the scripts

% Add the functionsRA directory to MATLAB path
addpath(scriptDir);

% Run the first MATLAB script
try
    run(fullfile(scriptDir, 'graphs_time_monthquart_US.m'));  
    disp('First MATLAB script ran successfully.');
catch ME
    fprintf('Error running first MATLAB script: %s\n', ME.message);
end


% Change directory to where the scripts are located
baseDir = pwd; % Get the directory where the script is running

cd(baseDir);

scriptDir = fullfile(baseDir, 'functionsRA'); % Define the folder containing the scripts

% Add the functionsRA directory to MATLAB path
addpath(scriptDir);
% Run the second MATLAB script
try
    run(fullfile(scriptDir, 'graphs_time_monthquart_EZ.m'));  
    disp('Second MATLAB script ran successfully.');
catch ME
    fprintf('Error running second MATLAB script: %s\n', ME.message);
end





%%

% ======= STEP 7: Conditional and Initial Distributions =======

baseDir = pwd; % Get the directory where the script is running

scriptDir = fullfile(baseDir, 'functionsRA'); % Define the folder containing the scripts

% Add the functionsRA directory to MATLAB path
addpath(scriptDir);


% Run the first MATLAB script
try
    run(fullfile(scriptDir, 'Conditional_Initial.m'));  
    disp('MATLAB script ran successfully.');
catch ME
    fprintf('Error running first MATLAB script: %s\n', ME.message);
end

%% OPTIONAL
clear all
load('./input/runsection.mat', 'runsection')

% % ======= STEP 14: Run Joint and Pareto for Standard errors estimation =======
if runsection==1
addpath('functionsRA');

% Call your functions
SE_EZ_Joint;
SE_EZ_Pareto;
SE_US_Joint;
SE_US_Pareto;

% Remove path after execution (optional)
rmpath('functionsRA');
end



%% Step 8
load('./input/rscriptPath.mat', 'rscriptPath');
baseDir = pwd; % Get the directory where the script is running
scriptDir = fullfile(baseDir, 'functionsRA'); % Define the folder containing the scripts

% Define paths to your Stata .do file
myRfile = fullfile(scriptDir, 'render_myRmd.R.');
myRfile = sprintf('"%s"', myRfile);


% Construct the command string that MATLAB executes
commandStr = sprintf('%s %s', rscriptPath, myRfile);  % Removed extra quotes in sprintf
[status, cmdout] = system(commandStr);

% Display the system output for troubleshooting
fprintf('Status: %d\nOutput:\n%s\n', status, cmdout);
%% Step 9
load('./input/rscriptPath.mat', 'rscriptPath');
baseDir = pwd; % Get the directory where the script is running
scriptDir = fullfile(baseDir, 'functionsRA'); % Define the folder containing the scripts

% Define paths to your Stata .do file
myRfile = fullfile(scriptDir, 'Pareto_Matrix.R.');
myRfile = sprintf('"%s"', myRfile);


% Construct the command string that MATLAB executes
commandStr = sprintf('%s %s', rscriptPath, myRfile);  % Removed extra quotes in sprintf
[status, cmdout] = system(commandStr);

% Display the system output for troubleshooting
fprintf('Status: %d\nOutput:\n%s\n', status, cmdout);



%%

% ======= STEP 10: Moving files and calling Figure_master to produce figures =======
baseDir = pwd; % Get the directory where the script is running
scriptDir = fullfile(baseDir, 'functionsRA'); % Define the folder containing the scripts

% Define paths to your Stata .do file
doFile1 = fullfile(scriptDir, '_figures_master.do');

% Define Stata executable path (modify according to your installation)
load('./input/statapath.mat', 'stataPath');
% Ensure MATLAB switches to the correct directory before launching Stata
cd(scriptDir);

% Run the first .do file
command1 = sprintf('%s /e do "%s"', stataPath, doFile1);
status1 = system(command1);

% Check if the first script ran successfully
if status1 == 0
    disp('Stata plots script ran successfully.');
else
    disp('Error running Stata script.');
end
%%
% ======= STEP 11: Call =======
baseDir = pwd; % Get the directory where the script is running
scriptDir = fullfile(baseDir, 'functionsRA'); % Define the folder containing the scripts

% Define paths to your Stata .do file
doFile1 = fullfile(scriptDir, 'SumNQMP_replication.do');

% Define Stata executable path (modify according to your installation)
load('./input/statapath.mat', 'stataPath');
% Ensure MATLAB switches to the correct directory before launching Stata
cd(scriptDir);

% Run the first .do file
command1 = sprintf('%s /e do "%s"', stataPath, doFile1);
status1 = system(command1);

% Check if the first script ran successfully
if status1 == 0
    disp('Stata plots script ran successfully.');
else
    disp('Error running Stata script.');
end




